package br.com.werp.suporte.Hardware;

import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import com.egen.util.jdbc.*;
import com.egen.util.text.*;

/**
 * Creation Date 03/09/2004 10:26:08
 * Last Modify Date 17/10/2006 14:49:45
 */

public class RedeDetalheAction extends com.egen.util.struts.AbstractAction {

  public ActionForward perform_update_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    HttpSession session = request.getSession(true);
    ActionForward actionForward = null;
    com.egen.util.jdbc.JdbcUtil j = null;
    try {
      j = new com.egen.util.jdbc.JdbcUtil();
      update_action(j, mapping, form, request, response);
      actionForward = mapping.findForward("voltar");
    } catch (Exception e) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("error.action.exception",
              (e)));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
      session.setAttribute("exception",
          (e));
      actionForward = mapping.findForward("same");
    }
    finally { if (j != null) {
          j.close();
        }
            } return actionForward;
  }
  private void update_action(JdbcUtil j, ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) throws Exception {
    HttpSession session = request.getSession(true);
    RedeDetalheActionForm f = (RedeDetalheActionForm) form;
    br.com.werp.suporte.dbobj.table.Rede table =
      new br.com.werp.suporte.dbobj.table.Rede();
    Vector sets = new Vector(10, 2);
    table.setRede_id(
      com.egen.util.text.FormatNumber.parseInt(f.getRede_id()));
    table.setRede_marca(f.getRede_marca());
    if (f.getRede_marca() != null) {
      sets.addElement("rede_marca");
    } else {
      sets.addElement("");
    };
    table.setRede_modelo(f.getRede_modelo());
    if (f.getRede_modelo() != null) {
      sets.addElement("rede_modelo");
    } else {
      sets.addElement("");
    };
    table.setRede_txtransferencia(f.getRede_txtransferencia());
    if (f.getRede_txtransferencia() != null) {
      sets.addElement("rede_txtransferencia");
    } else {
      sets.addElement("");
    };
    table.setRede_slot(f.getRede_slot());
    if (f.getRede_slot() != null) {
      sets.addElement("rede_slot");
    } else {
      sets.addElement("");
    };
    String[] set = new String[sets.size()];
    for (int i = 0; i < sets.size(); i++) {
      set[i] = (String) sets.elementAt(i);
    }
    //###############################auditoria.ALTERAR#########################
    int updated = 0;
    try {
      j.setAutoCommit(false);
      updated = j.update(table, set);
      if (updated == 0)
        throw new Exception("Nenhum registro atualizado!!");
      br.com.werp.regras.geral.Auditoria auditoria =
        br.com.werp.regras.geral.FabricaSubSistema.getAuditoria();
      String usuario = (String) session.getAttribute("j_username");
      auditoria.auditarRecurso(j.getConn(), usuario, table,
          auditoria.ALTERAR);
      j.commit();
    } catch (Exception e) {
      j.rollback();
      throw e;
    }
    finally{ j.setAutoCommit(true);
           } //###############################auditoria.ALTERAR#########################

    if (updated == 0) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("warn.norowsupdated",": " + updated));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
    } else {
      Vector res_Rede = (Vector) session.getAttribute("res_Rede");
      if (res_Rede != null) {
        res_Rede.setElementAt(table, 0);
        br.com.werp.suporte.dbobj.table.Rede r =
          (br.com.werp.suporte.dbobj.table.Rede)
          res_Rede.elementAt(0);
        f.setRede_id(r.getRede_id() + "");
        f.setRede_marca((r.getRede_marca() != null) ?
            r.getRede_marca() + "": "");
        f.setRede_modelo((r.getRede_modelo() != null) ?
            r.getRede_modelo() + "":"");
        f.setRede_txtransferencia((r.getRede_txtransferencia() != null) ?
            r.getRede_txtransferencia() + "":"");
        f.setRede_slot((r.getRede_slot() != null) ? r.getRede_slot() + "":
            "");
      }
    }
    session.setAttribute(mapping.getName(), form);
  }

  public ActionForward perform_delete_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    HttpSession session = request.getSession(true);
    ActionForward actionForward = null;
    com.egen.util.jdbc.JdbcUtil j = null;
    try {
      j = new com.egen.util.jdbc.JdbcUtil();
      delete_action(j, mapping, form, request, response);
      actionForward = mapping.findForward("voltar");
    } catch (Exception e) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("error.action.exception",
              (e)));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
      session.setAttribute("exception",
          (e));
      actionForward = mapping.findForward("same");
    }
    finally { if (j != null) {
          j.close();
        }
            } return actionForward;
  }
  private void delete_action(JdbcUtil j, ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) throws Exception {
    HttpSession session = request.getSession(true);
    RedeDetalheActionForm f = (RedeDetalheActionForm) form;
    br.com.werp.suporte.dbobj.table.Rede table =
      new br.com.werp.suporte.dbobj.table.Rede();
    table.setRede_id(
      com.egen.util.text.FormatNumber.parseInt(f.getRede_id()));
    table.setRede_marca(f.getRede_marca());
    table.setRede_modelo(f.getRede_modelo());
    table.setRede_txtransferencia(f.getRede_txtransferencia());
    table.setRede_slot(f.getRede_slot());
    //###############################auditoria.EXCLUIR#########################
    try {
      j.setAutoCommit(false);
      if (j.delete(table) == 0)
        throw new Exception("Nenhum registro apagado!!");
      br.com.werp.regras.geral.Auditoria auditoria =
        br.com.werp.regras.geral.FabricaSubSistema.getAuditoria();
      String usuario = (String) session.getAttribute("j_username");
      auditoria.auditarRecurso(j.getConn(), usuario, table,
          auditoria.EXCLUIR);
      j.commit();
    } catch (Exception e) {
      j.rollback();
      throw e;
    }
    finally{ j.setAutoCommit(true);
           } //###############################auditoria.EXCLUIR#########################
    if (false) {
      Vector res_Rede = (Vector) session.getAttribute("res_Rede");
      if (res_Rede != null) {
        res_Rede.removeElementAt(0);
        br.com.werp.suporte.dbobj.table.Rede r =
          (br.com.werp.suporte.dbobj.table.Rede)
          res_Rede.elementAt(0);
        f.setRede_id(r.getRede_id() + "");
        f.setRede_marca((r.getRede_marca() != null) ?
            r.getRede_marca() + "": "");
        f.setRede_modelo((r.getRede_modelo() != null) ?
            r.getRede_modelo() + "":"");
        f.setRede_txtransferencia((r.getRede_txtransferencia() != null) ?
            r.getRede_txtransferencia() + "":"");
        f.setRede_slot((r.getRede_slot() != null) ? r.getRede_slot() + "":
            "");
      }
    } else {
      f.reset(mapping, request);
    }
    session.setAttribute(mapping.getName(), form);
  }

  public ActionForward perform_back_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    HttpSession session = request.getSession(true);
    ActionForward actionForward = null;
    com.egen.util.jdbc.JdbcUtil j = null;
    try {
      j = new com.egen.util.jdbc.JdbcUtil();


      actionForward = mapping.findForward("voltar");
    } catch (Exception e) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("error.action.exception",
              (e)));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
      session.setAttribute("exception",
          (e));
      actionForward = mapping.findForward("same");
    }
    finally { if (j != null) {
          j.close();
        }
            } return actionForward;
  }

  public ActionForward perform_select_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    HttpSession session = request.getSession(true);
    ActionForward actionForward = null;
    com.egen.util.jdbc.JdbcUtil j = null;
    try {
      j = new com.egen.util.jdbc.JdbcUtil();
      select_action(j, mapping, form, request, response);
      actionForward = mapping.findForward("same");
    } catch (Exception e) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("error.action.exception",
              (e)));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
      session.setAttribute("exception",
          (e));
      actionForward = mapping.findForward("same");
    }
    finally { if (j != null) {
          j.close();
        }
            } return actionForward;
  }
  private void select_action(JdbcUtil j, ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) throws Exception {
    HttpSession session = request.getSession(true);
    RedeDetalheActionForm f = (RedeDetalheActionForm) form;
    br.com.werp.suporte.dbobj.table.Rede table =
      new br.com.werp.suporte.dbobj.table.Rede();
    String[][] select = null;
    String[] order = null;
    String[] groupby = null;
    String[] having = null;
    Object[][] where = { {"Rede_id","=",
        com.egen.util.text.FormatNumber.toInt(f.getRede_id())}, {"Rede_marca",
            "like", f.getRede_marca()}, {"Rede_modelo","like",
                f.getRede_modelo()}, {"Rede_txtransferencia","like",
                    f.getRede_txtransferencia()}, {"Rede_slot","like", f.getRede_slot()} };
    Vector res_Rede = j.select(table, select, where, groupby, having, order);
    session.setAttribute("res_Rede",res_Rede);
    if (res_Rede != null && res_Rede.size() > 0) {
      br.com.werp.suporte.dbobj.table.Rede r =
        (br.com.werp.suporte.dbobj.table.Rede)
        res_Rede.elementAt(0);
      f.setRede_id(r.getRede_id() + "");
      f.setRede_marca((r.getRede_marca() != null) ? r.getRede_marca() + "":
          "");
      f.setRede_modelo((r.getRede_modelo() != null) ?
          r.getRede_modelo() + "":"");
      f.setRede_txtransferencia((r.getRede_txtransferencia() != null) ?
          r.getRede_txtransferencia() + "":"");
      f.setRede_slot((r.getRede_slot() != null) ? r.getRede_slot() + "":"");
    } else {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("warn.norowsselected",""));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
    }
    session.setAttribute(mapping.getName(), form);
  }

}
